package com.bryanbibat.flashcards.robot;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.bryanbibat.flashcards.spring.Settings;


public class CommandParser {
  
  public CommandParser(String text) {
    
    settings = new Settings();
    id = null;
    
    Pattern base_regex = Pattern.compile("deal ([\\w]+)(( ([\\d]+) rounds)|( ([\\d]+) choices)|( ([\\d]+) penalty))*");
    Matcher matcher = base_regex.matcher(text);

    if (matcher.find()) {
      id = matcher.group(1);
      
      String options = text.substring(matcher.end(1), text.length());
      
      if (options != null) {
        Matcher rounds = Pattern.compile(" ([\\d]+) rounds").matcher(options);
        if (rounds.find()) {
          try {
            settings.setRounds(Integer.parseInt(rounds.group(1)));
          } catch (NumberFormatException e) {
          }
        }
        Matcher choices = Pattern.compile(" ([\\d]+) choices").matcher(options);
        if (choices.find()) {
          try {
            settings.setChoices(Integer.parseInt(choices.group(1)));
          } catch (NumberFormatException e) {
          }
        }
        Matcher penalty = Pattern.compile(" ([\\d]+) penalty").matcher(options);
        if (penalty.find()) {
          try {
            settings.setPenalty(Integer.parseInt(penalty.group(1)));
          } catch (NumberFormatException e) {
          }
        }
      }
    } 
    
    
  }
  
  private Settings settings;
  private String id;
  
  public Settings getSettings() {
    return settings;
  }

  public String getId() {
    return id;
  }
  
  
}
